package service;


import mapper.GoodsMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.*;
import util.SqlSessionFactoryUtils;

import java.util.List;

public class GoodsService  {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    public PageBean<Stock> selectByPageAndCondition(int currentPage, int pageSize, Stock stock) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        // 处理brand条件，模糊表达式

        String name = stock.getName();
        if (name != null && name.length() > 0) {
            stock.setName("%" + name + "%");
        }

        System.out.println(stock);
        //5. 查询当前页数据

        List<Stock> rows = mapper.selectByPageAndCondition(begin, size, stock);
        System.out.println(rows);

        //6. 查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(stock);
        System.out.println(totalCount);

        //7. 封装PageBean对象
        PageBean<Stock> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        System.out.println(pageBean);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }

    public PageBean<Invoice> selectByPageAndConditionB(int currentPage, int pageSize, Invoice invoice) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        // 处理brand条件，模糊表达式

        String name = invoice.getGname();
        if (name != null && name.length() > 0) {
            invoice.setGname("%" + name + "%");
        }


        List<Invoice> rows = mapper.selectByPageAndConditionB(begin, size, invoice);


        //6. 查询总记录数
        int totalCount = mapper.selectTotalCountByConditionB(invoice);


        //7. 封装PageBean对象
        PageBean<Invoice> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        System.out.println(pageBean);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }

    public List<InogBean> purchase(int id)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        List<InogBean> result = mapper.purchase(id);

        //释放资源
        sqlSession.close();
        return result;
    }

    public void inGoods(Invoice invoice)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper

        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.inGoods(invoice);

        sqlSession.commit();//提交事务

        //释放资源
        sqlSession.close();
    }

    public void outGoods(Invoice invoice)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper

        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.outGoods(invoice);

        sqlSession.commit();//提交事务

        //释放资源
        sqlSession.close();
    }


    public void makeInvoice(Invoice invoice)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper

        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.makeInvoice(invoice);

        sqlSession.commit();//提交事务

        //释放资源
        sqlSession.close();
    }

    public void makeOutInvoice(Invoice invoice)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper

        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.makeOutInvoice(invoice);

        sqlSession.commit();//提交事务

        //释放资源
        sqlSession.close();
    }

    public void dos(StockBean stockBean)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.dos(stockBean);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();

    }

    public void deleteGoods(int id)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.deleteGoods(id);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();

    }

    public void alterGoods(Goods goods)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.alterGoods(goods);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();

    }

    public void addGoods(Goods goods)
    {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        mapper.addGoods(goods);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();

    }

    public Admin login(String id, String password){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //4. 调用方法
        Admin user = mapper.select(id, password);

        //释放资源
        sqlSession.close();

        return  user;
    }


}
